package com.haima.ziliao.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haima.common.utils.SecurityUtils;
import com.haima.ziliao.domain.PDuwu;
import com.haima.ziliao.service.IPDuwuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.ziliao.domain.PMyduwu;
import com.haima.ziliao.service.IPMyduwuService;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 我的读物Controller
 * 
 * @author hima
 * @date 2024-11-15
 */
@RestController
@RequestMapping("/ziliao/myduwu")
public class PMyduwuController extends BaseController
{
    @Autowired
    private IPMyduwuService pMyduwuService;
    @Autowired
    private IPDuwuService duwuService;

    /**
     * 查询我的读物列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:myduwu:list')")
    @GetMapping("/list")
    public TableDataInfo list(PMyduwu pMyduwu)
    {
        startPage();
        List<PMyduwu> list = pMyduwuService.selectPMyduwuList(pMyduwu);
        return getDataTable(list);
    }

    @GetMapping("/listDuwu")
    public TableDataInfo listDuwu(PMyduwu pMyduwu)
    {
        startPage();
        List<PMyduwu> list = pMyduwuService.selectPMyduwuList(pMyduwu);

        for(int i=0;i<list.size();i++){
            PMyduwu pd = list.get(i);
            PDuwu pdw = duwuService.selectPDuwuById(pd.getDid());
            list.get(i).setDuwu(pdw);
        }
        return getDataTable(list);
    }

    @GetMapping("/getMyDuwuList")
    public TableDataInfo getMyDuwuList(PMyduwu pMyduwu)
    {
        pMyduwu.setUid(SecurityUtils.getUserId());
        startPage();
        List<PMyduwu> list = pMyduwuService.selectPMyduwuList(pMyduwu);
        for(int i=0;i<list.size();i++){
            Long did = list.get(i).getDid();
            PDuwu myduwu = duwuService.selectPDuwuById(did);
            list.get(i).setDuwu(myduwu);
        }
        return getDataTable(list);
    }

    @GetMapping("/getMyDuwuById")
    public AjaxResult getMyDuwuById(PMyduwu pMyduwu)
    {
        PMyduwu myduwu = pMyduwuService.getOne(new QueryWrapper<PMyduwu>().eq("uid", SecurityUtils.getUserId()).eq("did",pMyduwu.getDid()));
        return AjaxResult.success(myduwu);
    }

    @GetMapping("/getMyduwuNum")
    public AjaxResult getMyduwuNum()
    {
        Long num = pMyduwuService.count(new QueryWrapper<PMyduwu>().eq("uid", SecurityUtils.getUserId()).eq("uid",SecurityUtils.getUserId()));
        return AjaxResult.success(num);
    }

    /**
     * 导出我的读物列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:myduwu:export')")
    @Log(title = "我的读物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PMyduwu pMyduwu)
    {
        List<PMyduwu> list = pMyduwuService.selectPMyduwuList(pMyduwu);
        ExcelUtil<PMyduwu> util = new ExcelUtil<PMyduwu>(PMyduwu.class);
        util.exportExcel(response, list, "我的读物数据");
    }

    /**
     * 获取我的读物详细信息
     */
    @PreAuthorize("@ss.hasPermi('ziliao:myduwu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pMyduwuService.selectPMyduwuById(id));
    }

    /**
     * 新增我的读物
     */
    @PreAuthorize("@ss.hasPermi('ziliao:myduwu:add')")
    @Log(title = "我的读物", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PMyduwu pMyduwu)
    {
        return toAjax(pMyduwuService.insertPMyduwu(pMyduwu));
    }

    /**
     * 修改我的读物
     */
    @PreAuthorize("@ss.hasPermi('ziliao:myduwu:edit')")
    @Log(title = "我的读物", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PMyduwu pMyduwu)
    {
        return toAjax(pMyduwuService.updatePMyduwu(pMyduwu));
    }

    /**
     * 删除我的读物
     */
    @PreAuthorize("@ss.hasPermi('ziliao:myduwu:remove')")
    @Log(title = "我的读物", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pMyduwuService.deletePMyduwuByIds(ids));
    }
}
